package com.whmskj.xjlhbc.service.impl;

import cn.hutool.core.date.BetweenFormatter;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhbc.domain.DailyAttendanceLogMasterTable;
import com.whmskj.xjlhbc.domain.StopdataInfo;
import com.whmskj.xjlhbc.dto.*;
import com.whmskj.xjlhbc.mapper.KbMapper;
import com.whmskj.xjlhbc.mapper.StopdataInfoMapper;
import com.whmskj.xjlhbc.service.IKbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class KbServiceImpl implements IKbService {

    @Autowired
    private KbMapper kbMapper;

    @Autowired
    private StopdataInfoMapper stopdataInfoMapper;

    @Override
    public ClKbDto clKbRealInfo() {
        DateTime date = DateUtil.date();
        String now = date.toDateStr();
        int hour = date.hour(true);
        String classes = hour >= 9 && hour < 21 ? "早" : "晚";
        ClKbDto dto = Optional.ofNullable(kbMapper.clKbRealInfo(now)).orElseGet(ClKbDto::new);
        dto.setClasses(classes);
        return dto;
    }

    @Override
    public List<ClKbListDto> clKbDay(String date) {
        LocalDate end = StringUtils.hasText(date) ? LocalDate.parse(date) : LocalDate.now();
        LocalDate start = end.minusDays(30);
        List<String> dates = getDates(start, end);
        return dates.stream().map(item-> kbMapper.clKbDay(item)).collect(Collectors.toList());
    }

    @Override
    public List<ClKbListDto> clKbMonth(String month) {
        LocalDate end = StringUtils.hasText(month) ? LocalDate.parse(month+"-01") : LocalDate.now();
        LocalDate start = end.minusMonths(12);
        List<String> dates = getMonths(start, end);
        return dates.stream().map(item-> kbMapper.clKbMonth(item)).collect(Collectors.toList());
    }

    @Override
    public WsdKbDto wsdKb() {
        WsdKbDto dto = new WsdKbDto();
        WsdKbDto.WsdDto wd = dto.getWd();
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusDays(30);
        List<String> dates = getDates(start, end);
        List<String> data = Arrays.asList("织布车间", "准整车间");
        List<List<Double>> value = new ArrayList<>();
        data.forEach(d->{
            value.add(dates.stream().map(v -> 0.0).collect(Collectors.toList()));
        });
        wd.setXData(dates);
        wd.setData(data);
        wd.setValue(value);
        dto.setWd(wd);
        dto.setSd(wd);
        dto.setSj(DateUtil.now());
        return dto;
    }

    @Override
    public List<DailyAttendanceLogMasterTable> kqKbRtj() {
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusDays(30);
        List<String> dates = getDates(start, end);
        return dates.stream().map(date-> kbMapper.kqKbRtj(date)).collect(Collectors.toList());
    }

    @Override
    public List<DailyAttendanceLogMasterTable> kqKbYtj() {
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusMonths(12);
        List<String> dates = getMonths(start, end);
        return dates.stream().map(month-> kbMapper.kqKbYtj(month)).collect(Collectors.toList());
    }

    @Override
    public JtKbDto jtKb() {
        JtKbDto dto = new JtKbDto();
        JtKbDto.ZtDto zt = kbMapper.jtKbzt();
        JtKbDto.TjDto wbtj = kbMapper.jtKbwb();
        JtKbDto.TjDto wxtj = kbMapper.jtKbwx();
        List<JtKbDto.GzDto> gz = kbMapper.jtKbGz();
        List<JtKbDto.WxDto> wx = kbMapper.jtKbWx();
        JtKbDto.ChartDto chart = dto.getChart();
        LocalDate end = LocalDate.now();
        LocalDate start = end.minusDays(30);
        List<String> dates = getDates(start, end);
        List<Integer> value = dates.stream().map(date -> kbMapper.jtKbwbChart(date)).collect(Collectors.toList());
        chart.setXData(dates);
        chart.setValue(value);
        dto.setZt(zt);
        dto.setWbtj(wbtj);
        dto.setWxtj(wxtj);
        dto.setChart(chart);
        dto.setGz(gz);
        dto.setWx(wx);
        return dto;
    }

    @Override
    public List<ZzcjKbDto> zzcjRb(String date) {
        LocalDate end = StringUtils.hasText(date) ? LocalDate.parse(date) : LocalDate.now();
        LocalDate start = end.minusDays(30);
        List<String> dates = getDates(start, end);
        return dates.stream().map(val-> kbMapper.zzcjRb(val)).collect(Collectors.toList());
    }

    @Override
    public List<ZzcjKbDto> zzcjYb(String month) {
        LocalDate end = StringUtils.hasText(month) ? LocalDate.parse(month+"-01") : LocalDate.now();
        LocalDate start = end.minusMonths(12);
        List<String> dates = getMonths(start, end);
        return dates.stream().map(val-> kbMapper.zzcjYb(val)).collect(Collectors.toList());
    }

    @Override
    public List<String> zbcjRealInfo(int tab) {
        List<String> result = new ArrayList<>();
        if(tab ==1){
            ClKbDto dto = clKbRealInfo();
            result.add(dto.getNumberOfPersonsInAttendance());
            result.add(dto.getKts());
            result.add(dto.getZcl());
            result.add(dto.getXl());
        }
        if(tab ==2){
            JtKbDto.ZtDto zt = kbMapper.jtKbzt();
            result.add(zt.getYx());
            result.add(zt.getLt());
            result.add(zt.getDj());
            result.add(zt.getLx());
        }
        if(tab ==3){
            double min = 95;
            double max = 100;
            String xl = kbMapper.jtXl(min,max);
            result.add(xl);
            min = 90;
            max = 95;
            xl = kbMapper.jtXl(min,max);
            result.add(xl);
            min = 85;
            max = 90;
            xl = kbMapper.jtXl(min,max);
            result.add(xl);
            min = 0;
            max = 85;
            xl = kbMapper.jtXl(min,max);
            result.add(xl);
        }
        return result;
    }

    @Override
    public List<Map<String, String>> zbcjBzdb(String date) {
        date = StringUtils.hasText(date) ? date : LocalDate.now().toString();
        return kbMapper.zbcjBzdb(date);
    }

    @Override
    public List<ZbcjKbDto> zbcjRb(String date) {
        LocalDate end = StringUtils.hasText(date) ? LocalDate.parse(date) : LocalDate.now();
        LocalDate start = end.minusDays(30);
        List<String> dates = getDates(start, end);
        return dates.stream().map(val-> kbMapper.zbcjRb(val)).collect(Collectors.toList());
    }

    @Override
    public List<ZbcjKbDto> zbcjYb(String month) {
        LocalDate end = StringUtils.hasText(month) ? LocalDate.parse(month+"-01") : LocalDate.now();
        LocalDate start = end.minusMonths(12);
        List<String> dates = getMonths(start, end);
        return dates.stream().map(val-> kbMapper.zbcjYb(val)).collect(Collectors.toList());
    }

    @Override
    public List<Map<String, String>> zbcjJttj(String date) {
        date = StringUtils.hasText(date) ? date : LocalDate.now().toString();
        LambdaQueryWrapper<StopdataInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StopdataInfo::getTs,date);
        List<StopdataInfo> stopdataInfoList = stopdataInfoMapper.selectList(queryWrapper);
        List<Map<String, String>> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        long stopcount = stopdataInfoList.stream().mapToInt(StopdataInfo::getStopcount_weft_short).sum();
        long stoptime = stopdataInfoList.stream().mapToInt(StopdataInfo::getStoptime_weft_short).sum();
        map.put("name", "短纬停");
        map.put("value", String.valueOf(stopcount));
        map.put("time", DateUtil.formatBetween(stoptime*1000, BetweenFormatter.Level.SECOND));
        list.add(map);

        map = new HashMap<>();
        stopcount = stopdataInfoList.stream().mapToInt(StopdataInfo::getStopcount_weft_long).sum();
        stoptime = stopdataInfoList.stream().mapToInt(StopdataInfo::getStoptime_weft_long).sum();
        map.put("name", "长纬停");
        map.put("value", String.valueOf(stopcount));
        map.put("time", DateUtil.formatBetween(stoptime*1000, BetweenFormatter.Level.SECOND));
        list.add(map);


        map = new HashMap<>();
        stopcount = stopdataInfoList.stream().mapToInt(StopdataInfo::getStopcount_warp).sum();
        stoptime = stopdataInfoList.stream().mapToInt(StopdataInfo::getStoptime_warp).sum();
        map.put("name", "经停");
        map.put("value", String.valueOf(stopcount));
        map.put("time", DateUtil.formatBetween(stoptime*1000, BetweenFormatter.Level.SECOND));
        list.add(map);

        map = new HashMap<>();
        stopcount = stopdataInfoList.stream().mapToInt(StopdataInfo::getStopcount_other).sum();
        stoptime = stopdataInfoList.stream().mapToInt(StopdataInfo::getStoptime_other).sum();
        map.put("name", "其它");
        map.put("value", String.valueOf(stopcount));
        map.put("time", DateUtil.formatBetween(stoptime*1000, BetweenFormatter.Level.SECOND));
        list.add(map);

        map = new HashMap<>();
        stopcount = stopdataInfoList.stream().mapToInt(StopdataInfo::getStopcount_total).sum();
        stoptime = stopdataInfoList.stream().mapToInt(StopdataInfo::getStoptime_total).sum();
        map.put("name", "总计");
        map.put("value", String.valueOf(stopcount));
        map.put("time", DateUtil.formatBetween(stoptime*1000, BetweenFormatter.Level.SECOND));
        list.add(map);
        return list;
    }

    @Override
    public List<Map<String, Object>> zbcjXlph() {
        return kbMapper.zbcjXlph();
    }

    @Override
    public List<Map<String, String>> zbcjTjsc() {
        List<Map<String, String>> list = kbMapper.zbcjTjsc();
        list.forEach(l->{
            long time = NumberUtil.parseLong(l.get("time"));
            l.put("time", DateUtil.formatBetween(time * 1000, BetweenFormatter.Level.SECOND));
        });
        return list;
    }

    @Override
    public List<Map<String, String>> zbcjTj(String date) {
        Page.startPage();
        date = StringUtils.hasText(date) ? date : LocalDate.now().toString();
        List<Map<String, String>> list = kbMapper.zbcjTj(date);
        list.forEach(l->{
            long time = NumberUtil.parseLong(l.get("time"));
            l.put("time", DateUtil.formatBetween(time * 1000, BetweenFormatter.Level.SECOND));
        });
        return list;
    }

    private List<String> getDates(LocalDate start,LocalDate end){
        List<String> dates = new ArrayList<>();
        while(!start.isAfter(end)){
            dates.add(start.toString());
            start = start.plusDays(1);
        }
        return dates;
    }

    private List<String> getMonths(LocalDate start,LocalDate end){
        List<String> dates = new ArrayList<>();
        while(!start.isAfter(end)){
            dates.add(start.format(DateTimeFormatter.ofPattern("yyyy-MM")));
            start = start.plusMonths(1);
        }
        return dates;
    }
}
